package com.lyq.basic.service.impl;

import com.lyq.basic.mapper.BaseMapper;
import com.lyq.basic.query.BaseQuery;
import com.lyq.basic.service.IBaseService;
import com.lyq.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @Author:liangyunqian
 * @Date: 2022/12/20  21:38
 * @Description:
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T>{

    @Autowired
    private BaseMapper<T> baseMapper;

    @Transactional
    @Override
    public void insert(T t) {
        baseMapper.insert(t);
    }

    @Transactional
    @Override
    public void deleteById(Serializable id) {
        baseMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public List<T> getAll() {
        return baseMapper.getAll();
    }

    @Override
    public T getById(Serializable id) {
        return baseMapper.getById(id);
    }

    @Transactional
    @Override
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);
    }

    @Override
    public PageList<T> listPage(BaseQuery query) {
        //1.查询total
        Long total = baseMapper.selectTotal(query);
        if(total > 0){
            //2.查询rows
            List<T> rows = baseMapper.selectRows(query);
            //3.封装结果
            return new PageList<>(total,rows);
        }else {
            return new PageList<>();
        }
    }
}
